package cn.gcjxsc.service.merchant.recharge;

import cn.gcjxsc.common.consts.*;
import cn.gcjxsc.entity.mall.BaseOrder;
import com.zgxl.common.util.DateTimeUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 商户充值订单
 * @author pumbaa
 * @since 1.0
 */
@Service
public class RechargeOrderService {

    public BaseOrder createRecharge(Integer ownerId, BigDecimal amount) {
        BaseOrder baseOrder = new BaseOrder();
        baseOrder.setPayStatus(PayStatus.UNPAY);
        baseOrder.setCreated(System.currentTimeMillis()/1000);
        baseOrder.setDiscr(OrderDiscr.RECHARGE_ORDER);
        baseOrder.setOwnerId(ownerId);
        baseOrder.setPayAmount(amount);
        baseOrder.setStatus(OrderStatus.STATUS_NOT_PAID);
        baseOrder.setDoneTime(0L);
        baseOrder.setPayTime(0L);
        baseOrder.setCancelTime(0L);
        baseOrder.setExpiredTime(0L);
        baseOrder.setPayChannel(PayChannel.WECHAT);
        baseOrder.setPayType(PayType.ONLINE);
        baseOrder.setSn(createTradeNo(8));
        return baseOrder;
    }

    public String createTradeNo(int len) {
        String sn = DateTimeUtil.generateSn();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            sb.append((int) (Math.random() * 10));
        }
        return "8" + sn + sb.toString();
    }
}
